<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" :align-center="true" width="40%" :title="`配置用户当家币`">
    <el-form
      ref="ruleFormRef"
      :rules="rules"
      :disabled="dialogData.isView"
      :model="dialogData.itemInfo"
      label-width="100px"
      label-suffix=" :"
      :hide-required-asterisk="dialogData.isView"
    >
      <el-row :gutter="20">
        <el-col :span="22">
          <el-row>
            <el-col>
              <el-form-item label="可用额度">
                <el-input-number
                  style="width: 60%"
                  v-model="dialogData.itemInfo.showAvailableCnt"
                  placeholder="请填写可用额度"
                  :min="0"
                  clearable
                ></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row>
            <el-col>
              <el-form-item label="已用额度">
                <el-input-number
                  style="width: 60%"
                  v-model="dialogData.itemInfo.showUsedCnt"
                  placeholder="请填写已用额度"
                  :min="0"
                  clearable
                ></el-input-number>
              </el-form-item>
            </el-col>
          </el-row> -->
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" v-show="!dialogData.isView" @click="handleSubmit" :loading="btnLoading">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { getOptTitle, OptEnum } from "@/biz/eenum/appEnum";
import { DialogParam } from "@/biz/interface/DialogType";
import { CompanyUserConfigVo } from "@/biz/interface/CompanyType";
import { saveCompanyUserCoinApi } from "@/api/modules/cpy/usercoin";

const rules = reactive({
  phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }]
});

//页面的数据
interface UserDialogOption extends DialogParam {
  //弹出表单的标题
  title: string;
  //是否是查看状态，查看状态是不可以提交数据  isView: false,
  isView: boolean;
  //是否是审核状态
  isAudit: boolean;
  //当前的数据对象
  itemInfo: CompanyUserConfigVo;
}

//提交按钮，避免多次提交
const btnLoading = ref(false);
// 弹出框的参数
const dialogVisible = ref(false);
const dialogData = reactive<UserDialogOption>({
  optType: OptEnum.UNKNOW,
  title: "",
  isView: false,
  isAudit: false,
  itemInfo: {
    userId: "",
    userName: "",
    account: "",
    flagIdentity: "",
    flagActive: "Y",
    showAvailableCnt: 0,
    showUsedCnt: 0
  }
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogParam): void => {
  dialogData.optType = params.optType;
  dialogData.rowData = params.rowData;

  dialogData.isView = dialogData.optType === OptEnum.VIEW;
  dialogData.isAudit = dialogData.optType === OptEnum.AUDIT;
  dialogData.title = getOptTitle(dialogData.optType);

  if (dialogData.rowData) {
    dialogData.itemInfo = dialogData.rowData as unknown as CompanyUserConfigVo;
  }

  dialogVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  btnLoading.value = true;
  ruleFormRef.value!.validate(async valid => {
    if (!valid) {
      btnLoading.value = false;
      return;
    }
    try {
      if (dialogData.optType === OptEnum.EDIT) {
        //提交数据
        let res = await saveCompanyUserCoinApi({
          ...dialogData.itemInfo
        });
        if (res.data) {
          ElMessage.success({ message: `用户当家币配置成功！` });
          //刷新列表数据
          emits("refresh-list");
          dialogVisible.value = false;
        }
      }
    } catch (error) {
      // console.log(error);
    } finally {
      btnLoading.value = false;
    }
  });
};

//页面事件
const emits = defineEmits(["refresh-list"]);

//页面参数
defineExpose({
  acceptParams
});
</script>
